package git.soulbgm.utils;

import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtobufIOUtil;
import com.dyuproject.protostuff.Schema;
import com.dyuproject.protostuff.runtime.RuntimeSchema;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 贺瑞杰
 * @version V1.0
 * @data 2018年5月08日 下午1:26:48
 * @Description 使用protostuff 序列化和反序列化的工具类
 */
public class SerializeUtil {

    private static Map<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<Class<?>, Schema<?>>();

    private static <T> Schema<T> getSchema(Class<T> clazz) {
        @SuppressWarnings("unchecked")
        Schema<T> schema = (Schema<T>) cachedSchema.get(clazz);
        if (schema == null) {
            schema = RuntimeSchema.createFrom(clazz);
            if (schema != null) {
                cachedSchema.put(clazz, schema);
            }
        }
        return schema;
    }

    /**
     * 将对象通过protostuff的方式进行序列化得到byte[]
     *
     * @param t   要序列化的对象
     * @param <T>
     * @return
     */
    public static <T> byte[] serializer(T t) {
        Class<T> clazz = (Class<T>) t.getClass();
        LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
        try {
            Schema<T> schema = getSchema(clazz);
            return ProtobufIOUtil.toByteArray(t, schema, buffer);
        } catch (Exception e) {
            throw new IllegalStateException(e.getMessage(), e);
        } finally {
            buffer.clear();
        }
    }

    /**
     * 将通过protostuff的方式序列化的byte[] 进行反序列化成实体对象
     *
     * @param data  protostuff的方式序列化的byte[]数据
     * @param clazz 要进行反序列化成实体的Class
     * @param <T>
     * @return
     */
    public static <T> T deserializer(byte[] data, Class<T> clazz) {
        try {
            Schema<T> schema = getSchema(clazz);
            T t = schema.newMessage();
            ProtobufIOUtil.mergeFrom(data, t, schema);
            return t;
        } catch (Exception e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
    }

    /**
     * 序列化Map结构数据
     *
     * @param map
     * @param <V>
     * @return
     */
    public static <K, V> byte[] serializerMap(Map<K, V> map) {
        if (map == null) {
            return null;
        }
        MapDto mapDto = new MapDto(map);
        return serializer(mapDto);
    }

    /**
     * 反序列化Map
     *
     * @param data
     * @param <V>
     * @return
     */
    public static <K, V> Map<K, V> deserializerMap(byte[] data) {
        if (data == null || data.length == 0) {
            return null;
        }
        MapDto mapDto = deserializer(data, MapDto.class);
        if (mapDto != null) {
            return (Map<K, V>) mapDto.getMap();
        }
        return null;
    }

    /**
     * 序列化List结构数据
     *
     * @param list
     * @param <V>
     * @return
     */
    public static <V> byte[] serializerList(List<V> list) {
        if (list == null) {
            return null;
        }
        ListDto listDto = new ListDto(list);
        return serializer(listDto);
    }

    /**
     * 反序列化List
     *
     * @param data
     * @param <V>
     * @return
     */
    public static <V> List<V> deserializerList(byte[] data) {
        if (data == null || data.length == 0) {
            return null;
        }
        ListDto listDto = deserializer(data, ListDto.class);
        if (listDto != null) {
            return (List<V>) listDto.getList();
        }
        return null;
    }

    /**
     * Map的包装类
     */
    public static class MapDto {
        private Map<?, ?> map;

        public MapDto(Map<?, ?> map) {
            this.map = map;
        }

        public Map<?, ?> getMap() {
            return map;
        }

        public void setMap(Map<?, ?> map) {
            this.map = map;
        }
    }

    /**
     * List的包装类
     */
    public static class ListDto {
        private List<?> list;

        public ListDto(List<?> list) {
            this.list = list;
        }

        public List<?> getList() {
            return list;
        }

        public void setList(List<?> list) {
            this.list = list;
        }

    }

    public static class ObjectDto {
        private Object obj;

        public ObjectDto(Object obj) {
            this.obj = obj;
        }

        public Object getObj() {
            return obj;
        }

        public void setObj(Object obj) {
            this.obj = obj;
        }
    }

}
